package com.siki.controller;

import com.siki.config.filterConfig.jwtConfig.annotation.UserLoginToken;
import com.siki.dto.enterprise.EnterpriseSaveInfoDTO;
import com.siki.dto.enterprise.SaveTalentPositionDTO;
import com.siki.service.EnterpriseService;
import com.siki.utils.JsonInfoReturn;
import com.siki.vo.enterprise.EnterpriseUserInfoVO;
import com.siki.vo.enterprise.TalentPositionInfoVO;
import com.siki.vo.enterprise.TalentPositionListInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Author Siki
 * @Date 2020/6/7
 */
@RestController
@Api(tags = "企业用户相关操作模块")
@RequestMapping("/api/vi/siki/enterprise")
@CrossOrigin
@UserLoginToken
public class EnterpriseUserController {

    private final EnterpriseService enterpriseService;

    public EnterpriseUserController(EnterpriseService enterpriseService) {
        this.enterpriseService = enterpriseService;
    }

    @ApiOperation(value = "查询接口 - 查询当前企业用户详细信息",consumes = "application/json",httpMethod = "GET")
    @RequestMapping(value = "/get_info",method = RequestMethod.GET)
    public JsonInfoReturn getInfo(Authentication authentication){
        return JsonInfoReturn.success(new EnterpriseUserInfoVO(enterpriseService.findUserById(authentication.getName())));
    }

    @ApiOperation(value = "[提交接口] - 企业用户信息修改",consumes = "application/json",httpMethod = "POST")
    @RequestMapping(value = "save_info",method = RequestMethod.POST)
    public JsonInfoReturn saveInfo(@RequestBody @Validated @ApiParam("企业用户信息修改DTO")EnterpriseSaveInfoDTO dto,
                                   Authentication authentication){
        enterpriseService.saveInfo(dto,authentication.getName());
        return JsonInfoReturn.success();
    }

    @ApiOperation(value = "[查询接口] - 查询当前企业下岗位的相关信息",consumes = "application/json",
            httpMethod = "GET",response = TalentPositionListInfoVO.class)
    @RequestMapping(value = "/get_position_list",method = RequestMethod.GET)
    public JsonInfoReturn getPositionInfo(Authentication authentication){
        return JsonInfoReturn.success(enterpriseService.getPositionInfo(authentication.getName()).stream().map(TalentPositionListInfoVO::new));
    }

    @ApiOperation(value = "[查询接口] - 根据id查询岗位的详细信息", consumes = "application/json",
            httpMethod = "GET",response = TalentPositionInfoVO.class)
    @RequestMapping(value = "/get_info/{id}",method = RequestMethod.GET)
    public JsonInfoReturn getInfo(@PathVariable("id") @NotNull Long id){
        return JsonInfoReturn.success(new TalentPositionInfoVO(enterpriseService.getTalentPositionInfo(id)));
    }

    @ApiOperation(value = "[提交接口] - 企业发布岗位",consumes = "application/json",httpMethod = "POST")
    @RequestMapping(value = "/save_talent_position",method = RequestMethod.POST)
    public JsonInfoReturn saveTalentPosition(@RequestBody @Validated @ApiParam("保存岗位信息DTO") SaveTalentPositionDTO dto,Authentication authentication){
        enterpriseService.saveTalentPosition(dto,authentication.getName());
        return JsonInfoReturn.success();
    }

    @ApiOperation(value = "[删除接口] - 删除岗位信息",consumes = "application/json",httpMethod = "POST")
    @RequestMapping(value = "/delete_talent_position/{id}",method = RequestMethod.POST)
    public JsonInfoReturn deletePosition(@NotNull @PathVariable("id") Long id){
        enterpriseService.deletePosition(id);
        return JsonInfoReturn.success();
    }
}
